package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwgf.common.ViewCache;
import com.jwgf.faultnavigation.FaultNavigationMap;
import com.jwgf.fram.FramManager;
import com.jwgf.fram.FramManager.OnFramChangedListener;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.BitSet;


public class MachineStateCbfStateFragment extends BaseFragment implements OnFramChangedListener {
    public static final String fragmentTag = "MachineStateCbfStateFragment";
    public GifSurfaceView gifContainer;
    private View v;
    private int xiluolian_fault = 0;
    private int plc4Error331 = 0;
    private int plc4Error309 = 0;
    private int plc5Error352 = 0;
    private int plc5Error310 = 0;
    private int plc8Error308 = 0;
    private BitSet bitSet = new BitSet();

    //PLC 4号栈
    private int[][] errorArray4_307 = {{0x01, R.drawable.cbf_electric_alarm_vcro},//307.8    cbf_electric_alarm
            {0x02, R.drawable.cbf_empty_cop_full},  //307.9    cbf_empty_cop_full
            {0x04, R.drawable.cbf_piecing_alarm_vcro}, //307.10    cbf_piecing_alarm
            {0x08, R.drawable.cbf_after_piecing_alarm_vcro}, //307.11    cbf_after_piecing_alarm
            {0x10, R.drawable.cbf_cop_bypass_full_vcro}, //307.12   cbf_cop_bypass_full
            {0x20, R.drawable.cbf_remnant_cop_full_vcro},//307.13   cbf_remnant_cop_full
            {0x40, R.drawable.cbf_spinning_connect_vcro},//307.14   cbf_spinning_connect
            {0x80, R.drawable.cbf_spinning_fault_vcro},//307.15   cbf_spinning_fault
    };
    private int[][] errorArray4_331 = {{0, R.drawable.cbf_tube_ejection_fault},//331.0    拔管故障
            {0, R.drawable.cbf_empty_tube_channel_block},//331.1       空管通道堵塞
            {0, 0},//331.2
            {0, R.drawable.cbf_yarn_finder1_fault},//331.4     生头1故障
            {0, R.drawable.cbf_2nd_channel_block},//331.5     第2通道堵塞
            {0, R.drawable.cbf_yarn_finder2_fault},//331.7     cbf_piecing_2_fault
            {0, R.drawable.cbf_block_between_tube_ejection_and_tube_insert},//331.8    cbf_insert_to_draw_block
            {0, 0}//331.9    cbf_cop_insert_channel_block //插管通道阻塞报警
    };

    private int[][] errorArray4_309 = {
            {0, R.drawable.cbf_spindles_1_6_block},//D0309.6
            {0, R.drawable.cbf_spindles_1_6_block},//D0309.7
            {0, R.drawable.cbf_piecing_1_block},//D0308.E   cbf_piecing_1_block //生头1放行后阻塞
            {0, R.drawable.cbf_piecing_2_block},//D0308.F   cbf_piecing_2_block
            {0, R.drawable.cbf_spindles_1_6_block},//D0309.0      cbf_spindles_1_6_block
            {0, R.drawable.cbf_spindles_1_6_block},  //D0309.1      cbf_spindles_1_6_block
            {0, R.drawable.cbf_spindles_1_6_block}, //D0309.2      cbf_spindles_1_6_block
            {0, R.drawable.cbf_spindles_1_6_block}, //D0309.3
            {0, R.drawable.cbf_spindles_1_6_block}, //D0309.4
            {0, R.drawable.cbf_spindles_1_6_block},//D0309.5
    };

    //PLC 5号栈
    private int[][] errorArray5_352 = {{0x01, R.drawable.cbf_basin_fault},//D308.3       cbf_hopper_gate_fault
            {0x02, R.drawable.cbf_tube_insert_fault},  //D308.6      cbf_cop_insert_fault
            {0x04, R.drawable.cbf_no_yarn_in_yarn_magazine},
            {0x08, R.drawable.cbf_yarn_push_plate_fault}, //D308.A     cbf_push_plate_fault
            {0x10, R.drawable.cbf_block_between_tube_insert_to_tube_full_channel}, //D308.D    cbf_insert_to_full_block
            {0x20, 0},
            {0x40, 0},
            {0x80, R.drawable.cbf_hydraulic_motor_fault},
    };

    //	private int[][] errorArray5_310 = {{0x01,R.drawable.cbf_hopper_empty}};//D310.0       cbf_hopper_empty      cbf_hopper_gate_fault
    //PLC 8号栈
    //private int[][] errorArray8_308 = {{0x01,R.drawable.cbf_hopper_empty},//D310.0       cbf_hopper_empty      cbf_hopper_gate_fault
    //		};
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = ViewCache.getInstance().getView(R.layout.fragment_machine_state_cbf_state);
        gifContainer = (GifSurfaceView) v.findViewById(R.id.gif_container);
        gifContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bitSet.cardinality() != 0) {
                    int code = 0x04013300 | bitSet.nextSetBit(0);
                    EventBus.getDefault().post(FaultNavigationMap.getInstance().get(code));
                }
            }
        });
        refreshError();
        FramManager.getInstance().registerOnFramChangedListener(this);
        return v;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            gifContainer.setVisibility(View.INVISIBLE);
        } else {
            refreshError();
            gifContainer.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onFramChanged(FramManager framManager, String key) {
        if (key.equals("307")) {
            xiluolian_fault = framManager.getInt(key, 0);
            bitSet.clear();
            for (int i = 0; i < 16; i++) {
                int bit = (xiluolian_fault >> i) & 0x01;
                if (bit != 0) {
                    if (i >= 8 && i != 9) {
                        if (bitSet.cardinality() == 0) {
                            bitSet.set(i);
                        }
                    }
                }
            }
            refreshError();
        } else if (key.equals("331")) { //modified by penglion
            plc4Error331 = framManager.getInt(key, 0);
            refreshError();
        } else if (key.equals("332")) { //modified by penglion
            plc4Error309 = framManager.getInt(key, 0);
            refreshError();
        } else if (key.equals("352")) {
            plc5Error352 = framManager.getInt(key, 0);
            refreshError();
        }
    }

    public void refreshError() {
        ArrayList<Integer> list = new ArrayList<Integer>();
        String machineType = MachineParameterSharedPreference.getInstance().getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0");
        if (machineType.equals("3")) {
//            ImageView imageView = (ImageView) v.findViewById(R.id.cbf_status_bk);
////			imageView.setImageResource(R.drawable.cbf_error_bk);
//            imageView.setImageResource(R.drawable.xll_bk);
            list.add(R.drawable.xll_bk);
            for (int i = 0; i < errorArray4_307.length; i++) {
                if (((xiluolian_fault >> (8 + i)) & 0x01) != 0) {
                    list.add(errorArray4_307[i][1]);
                }
            }
        } else if (machineType.equals("5") || machineType.equals("2")) {
//            ImageView imageView = (ImageView) v.findViewById(R.id.cbf_status_bk);
//			imageView.setImageResource(R.drawable.cbf_error_bk_d);
//            imageView.setImageResource(R.drawable.cbf_bk);
            list.add(R.drawable.cbf_bk);
            for (int i = 0; i < errorArray4_331.length; i++) {
                if (i == 2 || i == 7)
                    continue;
                if (((plc4Error331 >> i) & 0x01) != 0) {
                    list.add(errorArray4_331[i][1]);
                }
            }

//            for (int i = 0; i < errorArray4_309.length; i++) {
//                if (i == 0 || i == 1) {
//                    if (((plc4Error309 >> i) & 0x01) != 0) {
//                        list.add(errorArray4_309[i][1]);
//                    }
//                } else {
//                    if (((plc4Error309 >> (i + 5)) & 0x01) != 0) {
//                        list.add(errorArray4_309[i][1]);
//                    }
//                }
//            }
            for (int i = 0; i < errorArray5_352.length; i++) {
                if (i == 5 || i == 6)
                    continue;
                if (((plc5Error352 >> (8 + i)) & 0x01) != 0) {
                    list.add(errorArray5_352[i][1]);
                }
            }
        }
        gifContainer.setMovieList(list);
    }
}
